
package com.scalar.chiptrack.operations.process.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.AssemblyProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.FabProcessSplitDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.TestProcessSplitDAO;
import com.scalar.chiptrack.operations.process.forms.AssemblyProcessSplitForm;
import com.scalar.chiptrack.operations.process.forms.FabProcessSplitForm;
import com.scalar.chiptrack.users.User;

public final class StageOneAction extends ChipTrackAction {

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {

		HttpSession ses = request.getSession(true);
		
		User user = (User) ses.getAttribute (SessionKeys.USER_KEY);
		if (user == null)
		{
			return (mapping.findForward ("login"));
		}
     	String step		= request.getParameter("step");
		String productId   = request.getParameter("productID");
   
       	ProductDao productDao  = new ProductDao();
		String baseProductName = ProductDao.getBaseProductName(productId);

		ses.setAttribute("productId",productId);
		ses.setAttribute("baseProductName",baseProductName);

		if(step.equals("Fab"))
		{	
			saveToken(request);
			return (mapping.findForward("Fab"));
		}
		else if(step.equals("Sort"))
		{
			FabProcessSplitDAO fabProcessSplitDAO = new FabProcessSplitDAO();
			FabProcessSplitForm fabProcessSplitForm = null;
			fabProcessSplitForm = fabProcessSplitDAO.getFabTrackId(productId);
			String [] fabProduct=fabProcessSplitForm.getFabProduct();
			String [] fabTrackId=fabProcessSplitForm.getFabTrackId();
			ArrayList fabTrackList = new ArrayList();

			for(int i=0;i<fabTrackId.length;i++)
			{
				fabTrackList.add(fabTrackId[i]);		
			}
			request.setAttribute("fabProduct",fabProduct);
			request.setAttribute("fabtrackList",fabTrackList);
			saveToken(request);
			return (mapping.findForward("Sort"));	
		}	
		else if(step.equals("Assembly"))
		{
			FabProcessSplitDAO fabProcessSplitDAO = new FabProcessSplitDAO();
			FabProcessSplitForm fabProcessSplitForm = null;
			fabProcessSplitForm = fabProcessSplitDAO.getFabTrackId(productId);
			String [] fabProduct=fabProcessSplitForm.getFabProduct();
			String [] fabTrackId=fabProcessSplitForm.getFabTrackId();
			ArrayList fabTrackList = new ArrayList();

			for(int i=0;i<fabTrackId.length;i++)
			{
				fabTrackList.add(fabTrackId[i]);		
			}
			ses.setAttribute("fabTrackId",fabTrackList);
			ses.setAttribute("fabProduct",fabProduct);
			saveToken(request);
			return (mapping.findForward("Assembly"));	
		}
		else if(step.equals("Test"))
		{
			FabProcessSplitDAO fabProcessSplitDAO = new FabProcessSplitDAO();
			TestProcessSplitDAO testProcessSplitDAO = new TestProcessSplitDAO();
			FabProcessSplitForm fabProcessSplitForm = null;
			fabProcessSplitForm = fabProcessSplitDAO.getFabTrackId(productId);
			String [] fabProduct=fabProcessSplitForm.getFabProduct();
			String [] fabTrackId=fabProcessSplitForm.getFabTrackId();
			AssemblyProcessSplitDAO assemblyProcessSplitDAO = new AssemblyProcessSplitDAO();
			AssemblyProcessSplitForm assemblyProcessSplitForm = null;
			assemblyProcessSplitForm = assemblyProcessSplitDAO.getAssemblyTrackId(fabTrackId);
			String [] assymbleyProduct=assemblyProcessSplitForm.getAssemblyProduct();
			String [] assemblyTrackId=assemblyProcessSplitForm.getAssemblyTrackId();

			ArrayList assemblyTrackList = new ArrayList();
			ArrayList testNameList = new ArrayList();
			HashMap assyHash = new HashMap();

			for(int i=0;i<assemblyTrackId.length;i++)
			{
				assemblyTrackList.add(assemblyTrackId[i]);
				assyHash.put(assemblyTrackId[i],assymbleyProduct[i]);
			}
			testNameList = testProcessSplitDAO.getTestNames();
			ses.setAttribute("assemblyHash",assyHash);
			ses.setAttribute("testNames",testNameList);
			ses.setAttribute("assemblyTrackList",assemblyTrackList);
			saveToken(request);
			return (mapping.findForward("Test"));	
		}	
        else
        {
	   		return (mapping.findForward("exists"));
	  	}
	}
}